package com.fch.sdlibrary.service.impl;

import com.fch.sdlibrary.entity.Book;
import com.fch.sdlibrary.entity.BookClazz;
import com.fch.sdlibrary.mapper.BookClazzMapper;
import com.fch.sdlibrary.mapper.BookMapper;
import com.fch.sdlibrary.service.BookService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: sdlibrarySpring
 * @description: 图书服务类
 * @CreatedBy: fch
 * @create: 2022-05-04 19:48
 **/
@Service
public class BookServiceImpl implements BookService {
    @Resource
    BookMapper bookMapper;
    @Resource
    BookClazzMapper bookClazzMapper;

    @Override
    public List<Book> queryAllBooks() {
        List<Book> books = new ArrayList<>();
        books = bookMapper.queryAllBooks();
        return books;
    }

    @Override
    public Book queryBookById(String id) {
        Book book = new Book();
        book = bookMapper.queryBookById(id);
        return book;
    }

    @Override
    public PageInfo<Book> queryAllBooksByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Book> books = new ArrayList<>();
        books = bookMapper.queryAllBooks();
        PageInfo<Book> pageInfo = new PageInfo<>(books);
        return pageInfo;
    }

    @Override
    public List<BookClazz> queryAllBookClazz() {
        List<BookClazz> bookClazzes = new ArrayList<>();
        bookClazzes = bookClazzMapper.queryAllBookClazz();
        return bookClazzes;
    }


    @Override
    public PageInfo<Book> searchBookByNameAndClazz(Book book, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Book> books = new ArrayList<>();
        books = bookMapper.searchBookByNameAndClazz(book);
        PageInfo<Book> pageInfo = new PageInfo<>(books);
        return pageInfo;
    }

    @Override
    public PageInfo<Book> searchBookByIsbn(Book book, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Book> books = new ArrayList<>();
        books = bookMapper.searchBookByIsbn(book);
        PageInfo<Book> pageInfo = new PageInfo<>(books);
        return pageInfo;
    }

    @Override
    public boolean canBorrow(String bookId) {
        Book book = new Book();
        book = bookMapper.queryBookById(bookId);
        if (book != null) {
            return book.getBookQuantity() > 0;
        }
        return false;
    }

    @Override
    public Book addToBook(Book book) {
        BookClazz bookClazz = bookClazzMapper.queryBookClazzByCode(book.getBookClassCode());
        book.setBookClassificationId(bookClazz.getClassificationId());
        if (bookMapper.addToBook(book) > 0)
            return book;
        else
            return null;
    }

    @Override
    public int updateImg(Book book) {
        return bookMapper.updateImg(book);
    }

    @Override
    public Book updateBook(Book book) {
        Integer newClazzId = bookClazzMapper.queryBookClazzByCode(book.getBookClassCode()).getClassificationId();
        book.setBookClassificationId(newClazzId);
        return bookMapper.updateBook(book) > 0 ? book : null;
    }
}
